package com.example.himalaya2.presenters;

import com.example.himalaya2.api.XimalayaApi;
import com.example.himalaya2.interfaces.IAlbumDetailPresenter;
import com.example.himalaya2.interfaces.IAlbumDetailViewCallback;
import com.example.himalaya2.utils.Constants;
import com.example.himalaya2.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import org.w3c.dom.ls.LSInput;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlbumDetailPresenter implements IAlbumDetailPresenter {

    private static final String TAG = "AlbumDetailPresenter";
    private List<IAlbumDetailViewCallback> mCallbacks = new ArrayList<>();
    private List<Track> mTracks = new ArrayList<>();

    private Album mTargetAlbum = null;
    // 当前的专辑的id
    private int mCurrentAlbumId = -1;
    // 当前页
    private int mCurrentPageIndex = 0;
    private XimalayaApi mXimalayaApi;

    private AlbumDetailPresenter (){}

    public static AlbumDetailPresenter sInstance = null;

    public static AlbumDetailPresenter getInstance() {
        if (sInstance == null) {
            synchronized (AlbumDetailPresenter.class) {
                if (sInstance == null) {
                    sInstance = new AlbumDetailPresenter();
                }
            }
        }
        return sInstance;
    }

    @Override
    public void onLoaderMore(List<Album> result) {

    }

    @Override
    public void loadMore() {
        // 去加载更多的内容
        mCurrentPageIndex ++ ;
        // 传入 ture 表示结果会追加到列表的 后方
        doLoaded(true);
    }

    @Override
    public void onRefreshMore(List<Album> result) {

    }

    private void doLoaded(final boolean isLoaderMore) {
        mXimalayaApi = XimalayaApi.getInstance();
        mXimalayaApi.getAlbumDetail(new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                if (trackList != null) {
                    LogUtil.d(TAG,"CURRENT THREAD: "+Thread.currentThread().getName());
                    List<Track> tracks = trackList.getTracks();
                    LogUtil.d(TAG,"track size: "+tracks.size());
                    if (isLoaderMore) {
                        // 加载更多
                        mTracks.addAll(mTracks.size() -1,tracks);
                        int size = tracks.size();
                        handlerLoaderMoreResult(size);
                    } else {
                        // 刷新
                        mTracks.addAll(0,tracks);
                    }
                    handlerAlbumDetailResult(mTracks);
                }
            }

            @Override
            public void onError(int i, String s) {
                if (isLoaderMore) {
                    mCurrentPageIndex--;
                }
                LogUtil.d(TAG,"ERROR CODE :"+i+"  ERROR_MSG :"+s);
                handlerError(i,s);
            }
            }, mCurrentAlbumId,mCurrentPageIndex);
    }


    /*
    * 处理加载更多的结果
    * */
    private void handlerLoaderMoreResult(int size) {
        for (IAlbumDetailViewCallback callback : mCallbacks) {
            callback.onLoaderMoreFinish(size);
        }
    }

    @Override
    public void getAlbum(int albumId, int page) {
        mTracks.clear();
        this.mCurrentAlbumId = albumId;
        this.mCurrentPageIndex = page;
        // 根据页码 和 id 获取列表
        doLoaded(false);
    }


    /*
    * 发生错误，就通知自己UI
    *
    * */
    private void handlerError(int i, String s) {
        for (IAlbumDetailViewCallback callback : mCallbacks) {
            callback.onNetworkError(i,s);
        }
    }

    private void handlerAlbumDetailResult(List<Track> tracks) {
        for (IAlbumDetailViewCallback mCallback : mCallbacks) {
            mCallback.onDetailListLoaded(tracks);
        }
    }

    @Override
    public void registerViewCallback(IAlbumDetailViewCallback callback) {
        if (!mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
            if (mTargetAlbum != null) {
                callback.onAlbumLoaded(mTargetAlbum);
            }
        }
    }

    @Override
    public void UnRegisterViewCallback(IAlbumDetailViewCallback callback) {
        mCallbacks.remove(callback);
    }



    public void setTargetAlbum(Album targetAlbum) {
        this.mTargetAlbum = targetAlbum;
    }
}
